package org.nicocube.airain.gwt.server;

import org.nicocube.airain.domain.client.character.GameCharacter;
import org.nicocube.airain.domain.client.data.StorageException;
import org.nicocube.airain.domain.client.gamedate.AbstractGameDate;
import org.nicocube.airain.gwt.client.rpc.GCService;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Service for {@link GameCharacter} manipulation
 * @author nicolas
 * @version 0.1
 */
public class GCServiceImpl extends RemoteServiceServlet implements GCService{
	// as for version 0.1
	private static final long serialVersionUID = 1L;

	public GameCharacter retrieveGameCharacter() {
		/*
		 * return the target 0.1 lonely GameCharacter
		 * to be changed in target 0.2
		 */
		try {
			GameCharacter result = StorageManager.getStorageProvider().getStorage(GameCharacter.class).retrieveByOrder(1L);
			if (result == null) throw new IllegalStateException("GameCharacter not found");
			return result;
		} catch (StorageException e) {
			Log.error("Error while retrieving GameCharacter",e);
			throw new IllegalStateException("Error while retrieving GameCharacter",e);
		}
	}

	@Override
	public GameCharacter updateGameCharacter(AbstractGameDate knownDate) {
		GameCharacter gc = retrieveGameCharacter();
		if (gc !=null && gc.getLivedate().equals(knownDate)) return null;
		else {
			return gc;
		}
	}

}
